package com.cjz.servlet;

import com.cjz.dao.DButil;
import com.cjz.domain.Elective;
import com.cjz.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ShowSelectedCourseServlet",urlPatterns = "/ShowSelectedCourseServlet")
public class ShowSelectedCourseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获取用户名
        String userName =((User) request.getSession().getAttribute("user")).getUserName();
        ObjectMapper mapper = new ObjectMapper();
        DButil db = new DButil();
            List<String> IdList = db.selectedCoursesList(userName);
            Map<String, Object> coursesMap = db.selectCourseMap(IdList);
            out.write(mapper.writeValueAsString(coursesMap));
            db.release();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
